package com.chenfan.mcn.extension.excel;

import cn.afterturn.easypoi.excel.entity.params.ExcelImportEntity;
import cn.afterturn.easypoi.excel.imports.ExcelImportService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.util.NumberToTextConverter;

import java.math.BigDecimal;

/**
 * 定制Excel导入服务
 *
 * @author wulg
 * @date 2021-10-12
 **/
public class CustomExcelImportService extends ExcelImportService {

    /**
     *
     * @param entity
     * @param object
     * @param value
     * @throws Exception
     */
    @Override
    public void setValues(ExcelImportEntity entity, Object object, Object value) throws Exception {
        // 去掉非包装类的异常情况
        if (value == null) {
            return;
        }
        if(value instanceof String) {
            value = StringUtils.trim(value.toString());
        } else if(value instanceof BigDecimal) {
            double result = ((BigDecimal) value).doubleValue();
            value = new BigDecimal(NumberToTextConverter.toText(result));
        }
        if (entity.getMethods() != null) {
            setFieldBySomeMethod(entity.getMethods(), object, value);
        } else {
            entity.getMethod().invoke(object, value);
        }
    }
}
